//
//  FXTMainTabBarViewController.m
//  OfferJob
//
//  Created by 郑州动力无限科技 on 2017/6/30.
//  Copyright © 2017年 郑州动力无限科技. All rights reserved.
//

#import "FXTMainTabBarViewController.h"
#import "FXTMineViewController.h"
#import "FXTNavViewController.h"
#import "FXTSendTaskViewController.h"
#import "FXTTaskViewController.h"
#import "FXTHomeViewController.h"
#import "FXTCustomTabBar.h"
@interface FXTMainTabBarViewController ()<FXTCustomTabBarDelegate>

@property(nonatomic,strong)FXTHomeViewController *homeVC;

@property(nonatomic,strong)FXTSendTaskViewController *sendTaskVC;

@property(nonatomic,strong)FXTTaskViewController *taskVC;

@property(nonatomic,strong)FXTMineViewController *meVC;

@property(nonatomic,strong)FXTCustomTabBar *customTabBar;
@end

@implementation FXTMainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupCustomTabBar];
    
    [self setupAllChildViewControllers];
    
    // Do any additional setup after loading the view.
}
-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}

#pragma mark - Private Methods


-(void)setupCustomTabBar {
    FXTCustomTabBar *customTabBar =[[FXTCustomTabBar alloc]init];
    CGFloat tabBarHeight =84;
    customTabBar.frame =CGRectMake(0, 49.0 -tabBarHeight, SCREEN_WIDTH, tabBarHeight);
    [self.tabBar addSubview:customTabBar];
    self.customTabBar =customTabBar;
    self.customTabBar.delegate =self;
}

/**
 *  初始化所有的子控制器
 */
- (void)setupAllChildViewControllers
{
    // 1.首页
    [self setupChildViewController:self.homeVC title:@"首页" imageName:@"icon_syxiche" selectedImageName:@"icon_syxichea" andIndex:0];
    
    // 2.发布任务
    [self setupChildViewController:self.taskVC title:@"发布" imageName:@"icon_syzixun" selectedImageName:@"icon_syzixuna" andIndex:1];
    
    // 3.推荐码
    [self setupChildViewController:self.sendTaskVC title:@"任务" imageName:@"icon_syewm" selectedImageName:@"icon_syewma" andIndex:2];
    
    // 4.用户信息
    [self setupChildViewController:self.meVC title:@"我的" imageName:@"icon_sywd" selectedImageName:@"icon_sywda" andIndex:3];
    
}

/**
 *  初始化一个子控制器
 
 */
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName andIndex:(NSInteger)index
{
    // 1.设置控制器的属性
    childVc.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 2.包装一个导航控制器
    FXTNavViewController *nav = [[FXTNavViewController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
    // 自定义TabBar的SubViews
    [self.customTabBar creatAllTabBarSubViews:childVc.tabBarItem andIndex:index];

}
#pragma mark - CMCustomTabBarDelegate

-(void)tabBar:(FXTCustomTabBar *)tabBar didSelectVC:(NSInteger)lastIndex andNext:(NSInteger)nextIndex {
    self.selectedIndex =nextIndex -kTabBarButtonBaseTag;
}

# pragma mark - Setter & Getter
-(FXTHomeViewController *)homeVC {
    if (!_homeVC) {
        _homeVC =[[FXTHomeViewController alloc]init];
    }
    return _homeVC;
}
-(FXTTaskViewController *)taskVC {
    if (!_taskVC) {
        _taskVC =[[FXTTaskViewController alloc]init];
    }
    return _taskVC;
}

-(FXTSendTaskViewController *)sendTaskVC {
    if (!_sendTaskVC) {
        _sendTaskVC =[[FXTSendTaskViewController alloc]init];
    }
    return _sendTaskVC;
}
-(FXTMineViewController *)meVC {
    if (!_meVC) {
        _meVC =[[FXTMineViewController alloc]init];
    }
    return _meVC;
}

@end
